<template>
	<el-card>
		<el-form ref="form" label-width="80px">

			<el-form-item label="考试名称:">
				<el-select v-model="shijuanId" placeholder="请选择考试科目" @change="change()">
					<el-option v-for="(item,index) in list" :key="item.id" :label="item.name" :value="item.id">

					</el-option>
				</el-select>
			</el-form-item>
			<el-form-item label="考试时间:">
				<span>60分钟</span>
				<span style="position: absolute;right: 10px;">{{nowTime}}</span>
			</el-form-item>

			<el-form-item v-if="isKao">
				<el-button v-if="shijuanId" type="primary" @click="ksxm()">进入考试</el-button>
			</el-form-item>
			<div v-if="aa">
				<span>您已考过该试卷，您的考试成绩为：</span><span style="color: red;">{{grade.grade}}</span><span>分，试卷及格线为：</span><span
					style="color: red;" v-text="grade.shijuan.jige"></span>
				<span>{{msg}}</span>
			</div>
			<div v-if="time">
				<span>{{ddd}}</span>
			</div>
		</el-form>
	</el-card>
</template>
<script>
	export default {
		data() {
			return {
				nowTime: "",
				fullscreenLoading: false,
				shijuanId: '',
				list: [],
				timu: [],
				isKao: false,
				aa: false,
				time: false,
				grade: "",
				msg: '',
			}
		},
		methods: {
			ksxm() {
				const loading = this.$loading({
					lock: true,
					text: '正在努力加载试卷,即将开始考试！',
					spinner: 'el-icon-loading',
					background: 'rgba(0, 0, 0, 0.7)'
				});
				this.$ajax.post("/Shijuan/chouti", {
					"id": this.shijuanId
				}).then(res => {
					var result = res.data
					console.log(result);
					if (result.success) {
						this.timu = result.data
						window.sessionStorage.setItem("shijuanId", this.shijuanId)
						window.sessionStorage.setItem("timu", JSON.stringify(this.timu))
						this.$router.push('/exam/ksym')
						loading.close()

					} else {
						this.$message.error("试卷格式错误！")
						loading.close()
					}
				})
			},
			/* time(){
				for(let i=0; i<data.rows.length; i++){
					var remindTime = this.data.rows[i].course.create_time;//自己选择的时间
					var remindTimea = this.data.rows[i].course.update_time;
					var str = remindTime.toString();        // toString
					var stra = remindTimea.toString();
					str = str.replace('/-/g', '/');         //去空格字符等
					stra = stra.replace('/-/g', '/');
					var oldTime = new Date(str).getTime();  //装date
					var oldTimea = new Date(stra).getTime();
				}
			}, */
			change() {
var keyi = false
				const loading = this.$loading({
					lock: true,
					text: '正在努力查找试卷，请稍后！',
					spinner: 'el-icon-loading',
					background: 'rgba(0, 0, 0, 0.7)'
				});
				
				this.$ajax.post("/Shijuan/findById", {
					"id": this.shijuanId
				}).then(res => {
					var result = res.data.data
					var starttime = result.update_time
					var endTime = result.create_time
					var date = new Date()
					starttime = new Date(Date.parse(starttime));
					endTime = new Date(Date.parse(endTime));
					if (date < starttime) {
						console.log("时间未到!");
						this.$alert('未到考试时间，请联系您的老师修改', {
							confirmButtonText: '确定',
							callback: action => {
								loading.close()
								this.isKao = false
								this.aa = false
							}
						});
						return;
					} else if (date > endTime) {
						console.log("试卷过期");
						this.$alert('试卷已过期，请联系您的老师修改', {
							confirmButtonText: '确定',
							callback: action => {
								loading.close()
								this.isKao = false
								this.aa = false
							}
						});
						return;
					}
				})
				keyi=true
				if (keyi) {

					this.$ajax.post("/Grade/find", {
						"shijuanId": this.shijuanId
					}).then(res => {
						console.log(res.data.success);
						if (!res.data.data) {
							this.isKao = true
							this.aa = false
							loading.close()
						} else {
							this.isKao = false
							this.grade = res.data.data

							this.$nextTick(() => {
								if (this.grade.grade < this.grade.shijuan.jige) {
									this.msg = "评判为不合格，请您到手机端进行补考！"

								} else {
									this.msg = "评判为合格，恭喜您！"
								}
								this.aa = true

							})
							loading.close()
						}
					})
				}
			},

		},
		created() {
			window.sessionStorage.removeItem("timu")
			window.sessionStorage.removeItem("shijuanId")
			this.$ajax.post("/Shijuan/list", {}).then(res => {
				this.list = res.data.data
			})
			console.log()
		}
	}
</script>
